import { Context } from 'koa';
const { getCourses, getCoursesByPage, getByname, getBynameandPage, getCourseById, createCourse, update, delCourse } = require('../service/course.service')
const { isEmpty, invalidVideoId } = require('../constants/err.type')

interface pageInfo {
    pageNum?: number,
    pageSize?: number
}

class CourseController {
    // 获取课程列表接口
    public async getCourse(ctx: Context) {
        try {
            let res: any = null;
            let { pageNum, pageSize }: pageInfo = ctx.query;
            if (pageNum && pageSize) {
                pageNum = Math.max(pageNum * 1, 1);
                pageSize = Math.max(pageSize * 1, 1);
                res = await getCoursesByPage(pageNum, pageSize);
            } else {
                res = await getCourses();
            }
            ctx.body = {
                code: 200,
                message: '获取课程列表成功',
                result: {
                    ...res
                }
            }
        } catch (error) {
            console.error('获取课程列表失败', error)
        }
    }
    // 根据关键词获取课程
    public async getCourseByname(ctx: Context) {
        try {
            let res: any = null;
            let { pageNum, pageSize }: pageInfo = ctx.query;
            const name = ctx.query.name
            if(!name) return ctx.app.emit('error', isEmpty, ctx)
            if(pageNum && pageSize) {
                pageNum = Math.max(pageNum * 1, 1);
                pageSize = Math.max(pageSize * 1, 1);
                res = await getBynameandPage(pageNum, pageSize, name);
            } else {
                res = await getByname(name);
            }
            ctx.body = {
                code: 200,
                message: '搜索课程类型列表成功',
                result: {
                    ...res
                }
            }
        } catch (error) {
            console.error('根据关键词获取课程类型列表失败', error)
        }
    }
    // 根据id获取课程
    public async getById(ctx: Context) {
        try {
            const id = ctx.params.id
            const res: any = await getCourseById(id);
            if(res) {
                ctx.body = {
                    code: 200,
                    message: '根据id获取课程成功',
                    result: res
                }
            } else {
                return ctx.app.emit('error', invalidVideoId, ctx)
            }
        } catch (error) {
            console.error('根据id获取课程失败', error)
        }
    }
    // 添加课程
    public async addCourse(ctx: Context) {
        try {
            const res = await createCourse(ctx.request.body)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `添加课程成功`,
                    id: res.id
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: `添加课程失败`
                }
            }
        } catch (error) {
            console.error('添加课程失败', error)
        }
    }
    // 更新课程
    public async updateCourse(ctx: Context) {
        try {
            const id = ctx.params.id
            const res = await update(id, ctx.request.body)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `更新课程成功`
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: `更新课程失败`
                }
            }
        } catch (error) {
            console.error('更新课程', error)
        }
    }
    // 删除课程
    public async deleteCourse(ctx: Context) {
        try {
            const id = ctx.params.id
            const res = await delCourse(id)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `删除课程成功`
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: `删除课程失败`
                }
            }
        } catch (error) {
            console.error('更新课程失败', error)
        }
    }
}

module.exports = new CourseController();